﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MyUtils;
using System.Threading;
namespace CoffeeManager
{
    public partial class ProgessNTP : DevExpress.XtraEditors.XtraForm
    {
        DateTime sysTime = new DateTime();
        NTPForm ntpForm;
        int i = 0;
        delegate void myDelegate();
        Thread tr1, tr2;
        public NTPForm NtpForm
        {
            get { return ntpForm; }
            set { ntpForm = value; }
        }
        protected override bool GetAllowSkin()
        {
            if (this.DesignMode) return false;
            return true;

        }
        public  ProgessNTP()
        {
            InitializeComponent();
            this.Load+=new EventHandler(ProgessNTP_Load);
            this.btnClose.Visible = false;
        }
        private void ProgessNTP_Load(object sender, EventArgs e)
        {

            NtpForm.Hide();
            progessbarNTP.Properties.Maximum = 100;
            progessbarNTP.Properties.ShowTitle = true; 
            ThreadStart ts = new ThreadStart(GetNetWorkTime);
            tr1 = new Thread(ts);
            tr1.Start();
           
          
        }
        void GetNetWorkTime()
        {
            sysTime = NtpClient.GetNetworkTime();
            if (!sysTime.Equals(new DateTime()))
            {
                ThreadStart pts = new ThreadStart(Thread_SetProgessbar);
                tr2 = new Thread(pts);
                tr2.Start();
                tr2.Join(3000);
            }
            else
            {
                //Notify , connection is fail,  
                MyMessageBoxResults result = MyMessageBox.Show("Can't connect to any NTP server. Software will use current system time.", "Error", MyMessageBoxButtons.OK, MyMessageBoxIcons.Error);
                if (result.Equals(MyMessageBoxResults.OK))
                {
                    this.btnClose.Visible = true;
                }
                   
            }
          
        }
        void Thread_SetProgessbar()
        {
            for (i = 1; i <= 100; i++)
            {
                if (progessbarNTP.InvokeRequired)
                {
                    progessbarNTP.Invoke(new myDelegate(SetProgessbarvalue));
                }
                else
                {
                    SetProgessbarvalue();

                }
              
               
            }
            SYSTEMTIME systime = new SYSTEMTIME();
            systime.wYear = (ushort)sysTime.Year;
            systime.wMonth = (ushort)sysTime.Month;
            systime.wDayOfWeek = (ushort)sysTime.DayOfWeek;
            systime.wDay = (ushort)sysTime.Day;
            systime.wHour = (ushort)(sysTime.Hour);
            systime.wMinute = (ushort)sysTime.Minute;
            systime.wSecond = (ushort)sysTime.Second;
            systime.wMilliseconds = (ushort)sysTime.Millisecond;
            WinApi.Win32SetSystemTime(ref systime); 
            //Successful NTP 
            progessbarNTP.Invoke(new myDelegate(EnableCloseButton));
        }
        void SetProgessbarvalue()
        {
            progessbarNTP.EditValue = i;
            Thread.Sleep(20);
            
        }

        private void progessbarNTP_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
        {
            string val = e.Value.ToString();
            e.DisplayText = "The progress is: " + val;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
         void EnableCloseButton()
        {

            this.Close();
        }
   
    }
}
